/*
 * =============================================================
 *
 *        Project:  apue
 *
 *       Filename:  f4_21.c
 *
 *         Editor:  vim :set et ts=2 sts=2 sw=2
 *
 *         Author:  Martin Y. Yang , yang@libpix.org
 *
 *    Description:  
 *
 * =============================================================
 */
/**
 * @file
 * @brief Figure 4.21 Example of utime function
 */

#include "apue.h"
#include <fcntl.h>
#include <utime.h>

int zap_main(int argc, char *argv[])
{
  int i,fd;
  struct stat statbuf;
  struct utimbuf timebuf;

  for (i = 1; i < argc; i++) {
    if (stat(argv[i], &statbuf) < 0) {
      err_ret("%s: stat error", argv[i]);
      continue;
    }
    if ((fd = open(argv[i], O_RDWR | O_TRUNC)) < 0) {
      err_ret("%s: open error", argv[i]);
      continue;
    }
    close(fd);
    timebuf.actime = statbuf.st_atime;
    timebuf.modtime = statbuf.st_mtime;
    if (utime(argv[i], &timebuf) < 0) {
      err_ret("%s: utime error", argv[i]);
      continue;
    }
  }
  exit(0);
}

